using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JGSY.CMS.LowCode.Platform.Application.DTOs;
using JGSY.CMS.LowCode.Platform.Domain.Entities;

namespace JGSY.CMS.LowCode.Platform.Application.Interfaces
{
    /// <summary>
    /// 操作日志应用服务接口
    /// 提供操作日志的业务逻辑操作
    /// </summary>
    public interface IOperationLogAppService
    {
        /// <summary>
        /// 记录操作日志
        /// </summary>
        /// <param name="operationType">操作类型</param>
        /// <param name="operationName">操作名称</param>
        /// <param name="description">操作描述</param>
        /// <param name="userId">用户ID</param>
        /// <param name="userName">用户名</param>
        /// <param name="tenantId">租户ID</param>
        /// <param name="correlationId">关联ID</param>
        /// <param name="requestPath">请求路径</param>
        /// <param name="httpMethod">HTTP方法</param>
        /// <param name="duration">执行时长</param>
        /// <param name="success">是否成功</param>
        /// <param name="additionalData">额外数据</param>
        /// <returns>操作日志ID</returns>
        Task<long> LogOperationAsync(
            string operationType,
            string operationName,
            string? description = null,
            string? userId = null,
            string? userName = null,
            string? tenantId = null,
            string? correlationId = null,
            string? requestPath = null,
            string? httpMethod = null,
            long? duration = null,
            bool success = true,
            Dictionary<string, object>? additionalData = null);

        /// <summary>
        /// 记录用户操作日志
        /// </summary>
        /// <param name="context">操作上下文</param>
        /// <returns>操作日志ID</returns>
        Task<long> LogUserOperationAsync(OperationContext context);

        /// <summary>
        /// 获取操作日志
        /// </summary>
        /// <param name="id">操作日志ID</param>
        /// <returns>操作日志DTO</returns>
        Task<OperationLogDto?> GetOperationLogAsync(long id);

        /// <summary>
        /// 获取操作日志列表
        /// </summary>
        /// <param name="tenantId">租户ID</param>
        /// <param name="userId">用户ID</param>
        /// <param name="operationType">操作类型</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="success">是否成功</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>操作日志列表</returns>
        Task<(List<OperationLogDto> Items, int TotalCount)> GetOperationLogsAsync(
            string? tenantId = null,
            string? userId = null,
            string? operationType = null,
            DateTime? startTime = null,
            DateTime? endTime = null,
            bool? success = null,
            int pageIndex = 1,
            int pageSize = 20);

        /// <summary>
        /// 根据关联ID获取操作日志
        /// </summary>
        /// <param name="correlationId">关联ID</param>
        /// <returns>操作日志列表</returns>
        Task<List<OperationLogDto>> GetOperationLogsByCorrelationIdAsync(string correlationId);

        /// <summary>
        /// 获取用户操作统计
        /// </summary>
        /// <param name="tenantId">租户ID</param>
        /// <param name="userId">用户ID</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>操作统计信息</returns>
        Task<OperationStatistics> GetOperationStatisticsAsync(
            string? tenantId = null,
            string? userId = null,
            DateTime? startTime = null,
            DateTime? endTime = null);

        /// <summary>
        /// 获取性能统计信息
        /// </summary>
        /// <param name="tenantId">租户ID</param>
        /// <param name="operationType">操作类型</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>性能统计信息</returns>
        Task<PerformanceStatistics> GetPerformanceStatisticsAsync(
            string? tenantId = null,
            string? operationType = null,
            DateTime? startTime = null,
            DateTime? endTime = null);

        /// <summary>
        /// 批量记录操作日志
        /// </summary>
        /// <param name="operations">操作列表</param>
        /// <returns>记录的日志数量</returns>
        Task<int> LogBatchOperationsAsync(IEnumerable<OperationContext> operations);
    }

    /// <summary>
    /// 操作上下文信息
    /// </summary>
    public class OperationContext
    {
        public string OperationType { get; set; } = string.Empty;
        public string OperationName { get; set; } = string.Empty;
        public string? Description { get; set; }
        public string? UserId { get; set; }
        public string? UserName { get; set; }
        public string? TenantId { get; set; }
        public string? CorrelationId { get; set; }
        public string? TraceId { get; set; }
        public string? RequestPath { get; set; }
        public string? HttpMethod { get; set; }
        public string? IpAddress { get; set; }
        public string? UserAgent { get; set; }
        public long? Duration { get; set; }
        public bool Success { get; set; } = true;
        public string? ErrorMessage { get; set; }
        public Dictionary<string, object>? RequestData { get; set; }
        public Dictionary<string, object>? ResponseData { get; set; }
        public Dictionary<string, object>? AdditionalData { get; set; }
        public DateTime Timestamp { get; set; } = DateTime.UtcNow;
    }

    /// <summary>
    /// 操作统计信息
    /// </summary>
    public class OperationStatistics
    {
        public int TotalOperations { get; set; }
        public int SuccessfulOperations { get; set; }
        public int FailedOperations { get; set; }
        public Dictionary<string, int> OperationTypeCount { get; set; } = new();
        public Dictionary<string, int> UserOperationCount { get; set; } = new();
        public Dictionary<string, int> OperationsByHour { get; set; } = new();
        public double AverageDuration { get; set; }
        public long MaxDuration { get; set; }
        public long MinDuration { get; set; }
    }

    /// <summary>
    /// 性能统计信息
    /// </summary>
    public class PerformanceStatistics
    {
        public double AverageResponseTime { get; set; }
        public long MaxResponseTime { get; set; }
        public long MinResponseTime { get; set; }
        public int TotalRequests { get; set; }
        public Dictionary<string, double> AverageResponseTimeByOperation { get; set; } = new();
        public Dictionary<string, int> RequestCountByHour { get; set; } = new();
        public List<SlowOperationInfo> SlowestOperations { get; set; } = new();
    }

    /// <summary>
    /// 慢操作信息
    /// </summary>
    public class SlowOperationInfo
    {
        public string OperationName { get; set; } = string.Empty;
        public long Duration { get; set; }
        public DateTime Timestamp { get; set; }
        public string? UserId { get; set; }
        public string? RequestPath { get; set; }
    }
}
